package com.patrol.patrol;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Rect;

import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.patrol.data.EventImgModel;
import com.patrol.patrol.data.EventModel;
import com.patrol.patrol.utils.PickerDialog;
import com.sanders.db.DBContext;
import com.sanders.db.DBProxy;
import com.sanders.db.OnDBUpgrade;

import org.jaaksi.pickerview.dialog.IGlobalDialogCreator;
import org.jaaksi.pickerview.dialog.IPickerDialog;
import org.jaaksi.pickerview.picker.BasePicker;
import org.jaaksi.pickerview.util.Util;
import org.jaaksi.pickerview.widget.DefaultCenterDecoration;
import org.jaaksi.pickerview.widget.PickerView;

import butterknife.ButterKnife;

public class MyApplication extends Application {

    public static MyApplication getMyApplication() {
        return myApplication;
    }

    private static  MyApplication myApplication;

    public static DBProxy dbProxy;

    @Override
    public void onCreate() {
        super.onCreate();

        myApplication = this;
        ARouter.openLog();
        ARouter.init(myApplication);
        ButterKnife.setDebug(true);
        initDefaultPicker();
        initDB();
    }

    private void initDefaultPicker() {
        // 利用修改静态默认属性值，快速定制一套满足自己app样式需求的Picker.
        // BasePickerView
        PickerView.sDefaultVisibleItemCount = 5;
        PickerView.sDefaultItemSize = 50;
        PickerView.sDefaultIsCirculation = false;
        //PickerView.sDefaultDrawIndicator = false;

        // PickerView
        PickerView.sOutTextSize = 18;
        PickerView.sCenterTextSize = 18;
        PickerView.sCenterColor = Color.RED;
        PickerView.sOutColor = Color.GRAY;
        //PickerView.sShadowColors = null;

        // BasePicker
        int padding = Util.dip2px(this, 20);
        BasePicker.sDefaultPaddingRect = new Rect(padding, padding, padding, padding);
        BasePicker.sDefaultPickerBackgroundColor = Color.WHITE;
        BasePicker.sDefaultCanceledOnTouchOutside = false;
        //// 自定义 TopBar
        //DefaultPickerDialog.sDefaultTopBarCreator = new IDefaultTopBarCreator() {
        //  @Override public ITopBar createDefaultTopBar(LinearLayout parent) {
        //    return new CustomTopBar(parent);
        //  }
        //};
        BasePicker.sDefaultDialogCreator = new IGlobalDialogCreator() {
            @Override
            public IPickerDialog create(Context context) {
                return new PickerDialog();
            }
        };

        // DefaultCenterDecoration
        DefaultCenterDecoration.sDefaultLineWidth = 1;
        DefaultCenterDecoration.sDefaultLineColor = Color.RED;
        //DefaultCenterDecoration.sDefaultDrawable = new ColorDrawable(Color.WHITE);
        int leftMargin = Util.dip2px(this, 10);
        int topMargin = Util.dip2px(this, 2);
        DefaultCenterDecoration.sDefaultMarginRect =
                new Rect(leftMargin, -topMargin, leftMargin, -topMargin);
    }

    private void initDB(){
        //使用DBContext自动化创建表，每个表对应一个继承IDColumn.java类的POJO实体
        DBContext dbContext = new DBContext("anguan", 2,new OnDBUpgrade() {
            @Override
            public boolean onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                //return false 自己处理升级，return true 自动处理
                return true;
            }
        });
        dbContext.addTableBean(EventModel.class).addTableBean(EventImgModel.class);
        dbProxy = dbContext.buildDBProxy(this);
    }

}
